package grails.company

class EmployeeController {
    def list() {
        def employees = Employee.list()
        if (employees) {
            render(contentType: 'text/json') {
                success = true
                data = employees.collect {
                    [
                            id: it.id,
                            firstName: it.firstName,
                            lastName: it.lastName,
                            emailAddress: it.emailAddress,
                            accountName: it.accountName,
                            vatId: it.vatId,
                            cardNumber: it.cardNumber,
                            parentId: it.project.id
                    ]
                }
            }
        } else {
            render(contentType: 'text/json') { success = false }
        }
    }


    def view() {
        if (!params?.data?.id) {
            list()
            return
        }

        def emp = Employee.findById(params.data.id)
        if (emp) {
            render(contentType: 'text/json') {
                success = true
                data = {
                    id = emp.id
                    firstName = emp.firstName
                    lastName = emp.lastName
                    emailAddress = emp.emailAddress
                    accountName = emp.accountName
                    vatId = emp.vatId
                    cardNumber = emp.cardNumber
                    parentId = emp.project.id
                }
            }
        } else {
            render(contentType: 'text/json') { success = false }
        }
    }

    def update() {
        if (!params?.data?.id) {
            render(contentType: 'text/json') { success = false }
            return
        }

        def emp = Employee.findById(params.data.id)
        if (!emp) {
            render(contentType: 'text/json') { success = false }
            return
        }

        emp.properties['firstName', 'lastName', 'emailAddress', 'accountName', 'vatId', 'cardNumber'] = params.data
        if (!emp.validate()) {
            render(contentType: 'text/json') { success = false }
            return
        }

        emp.save()

        render(contentType: 'text/json') { success = true }
    }

    def delete() {
        if (!params?.data?.id) {
            render(contentType: 'text/json') { success = false }
            return
        }

        def emp = Employee.findById(params.data.id);
        if (emp) {
            emp.delete()
            render(contentType: 'text/json') { success = true }
        } else {
            render(contentType: 'text/json') { success = false }
        }
    }

    def create() {
        if (!params?.data?.parentId) {
            render(contentType: 'text/json') { success = false }
            return
        }

        def project = Project.findById(params.data.parentId)
        if (!project) {
            render(contentType: 'text/json') { success = false }
            return
        }

        def employee = new Employee();
        employee.properties['firstName', 'lastName', 'accountName', 'vatId', 'cardNumber'] = params.data
        employee.project = project
        employee.emailAddress = "$employee.accountName@${project.department.organization.title}.com"

        if (employee.validate() && employee.save()) {
            project.addToEmployees(employee)
            render(contentType: 'text/json') {
                success = true
                data = {
                    id = employee.id
                    firstName = employee.firstName
                    lastName = employee.lastName
                    emailAddress = employee.emailAddress
                    accountName = employee.accountName
                    parentId = employee.project.id
                }
            }
        } else {
            render(contentType: 'text/json') { success = false }
        }
    }
}
